\chapter{HT04 }
\hypertarget{md__c_1_2_users_2_a_s_u_s_2_desktop_2dm-ctrl_h7-balance-9025test_2modules_2motor_2_h_tmotor_2_h_t04}{}\label{md__c_1_2_users_2_a_s_u_s_2_desktop_2dm-ctrl_h7-balance-9025test_2modules_2motor_2_h_tmotor_2_h_t04}\index{HT04@{HT04}}

\begin{DoxyItemize}
\item 在通过串口设置电机的时候，注意发送指令不要追加回车~\newline
，否则电机端不响应。可以使用sscom，其会将你的按键直接当作ascii字符发送出去。
\item {\bfseries{此电机的\+CAN线序和\+Robo\+Master开发板相反，注意单独制作\+CAN线}}
\item 电机控制和反馈报文： 
\end{DoxyItemize}

注意，我们的代码实现不使用其电调协议的位置\+PD算法；自行实现了三环，并在最后给出电流参考值，发送给电调。因此command packed structure中的\texorpdfstring{$\ast$}{*}\texorpdfstring{$\ast$}{*}pos cmd \& vel cmd \& Kp \& Kd均为零\texorpdfstring{$\ast$}{*}\texorpdfstring{$\ast$}{*}。报文均为小端，\texorpdfstring{$\ast$}{*}\texorpdfstring{$\ast$}{*}低位在前\texorpdfstring{$\ast$}{*}\texorpdfstring{$\ast$}{*}。

请注意发送和反馈数据的\texorpdfstring{$\ast$}{*}\texorpdfstring{$\ast$}{*}单位\texorpdfstring{$\ast$}{*}\texorpdfstring{$\ast$}{*}。

\begin{quote}
\sout{HT的电机协议做的真不行，纯纯直接抄mit还抄不明白，之后全部换成\+LK！} \end{quote}


另一种上电时校准编码器的方法\+:


\begin{DoxyCode}{0}
\DoxyCodeLine{\textcolor{keyword}{static}\ \textcolor{keywordtype}{void}\ HTMotorDecode(CANInstance\ *motor\_can)}
\DoxyCodeLine{\{}
\DoxyCodeLine{\ \ \ \ uint16\_t\ tmp;\ \textcolor{comment}{//\ 用于暂存解析值,稍后转换成float数据,避免多次创建临时变量}}
\DoxyCodeLine{\ \ \ \ uint8\_t\ *rxbuff\ =\ motor\_can-\/>rx\_buff;}
\DoxyCodeLine{\ \ \ \ \mbox{\hyperlink{struct_h_t_motor___measure__t}{HTMotor\_Measure\_t}}\ *measure\ =\ \&((\mbox{\hyperlink{struct_h_t_motor_instance}{HTMotorInstance}}\ *)motor\_can-\/>id)-\/>measure;\ \textcolor{comment}{//\ 将can实例中保存的id转换成电机实例的指针}}
\DoxyCodeLine{}
\DoxyCodeLine{\ \ \ \ measure-\/>last\_angle\ =\ measure-\/>total\_angle;}
\DoxyCodeLine{\ \ \ \ tmp\ =\ (uint16\_t)((rxbuff[1]\ <<\ 8)\ |\ rxbuff[2]);}
\DoxyCodeLine{\ \ \ \ measure-\/>total\_angle\ =\ uint\_to\_float(tmp,\ P\_MIN,\ P\_MAX,\ 16)\ -\/\ measure-\/>angle\_bias;}
\DoxyCodeLine{}
\DoxyCodeLine{\ \ \ \ tmp\ =\ (uint16\_t)(rxbuff[3]\ <<\ 4)\ |\ (rxbuff[4]\ >>\ 4);}
\DoxyCodeLine{\ \ \ \ measure-\/>speed\_rads\ =\ AverageFilter((uint\_to\_float(tmp,\ V\_MIN,\ V\_MAX,\ 12)\ -\/\ HT\_SPEED\_BIAS),\ measure-\/>speed\_buff,\ SPEED\_BUFFER\_SIZE);}
\DoxyCodeLine{}
\DoxyCodeLine{\ \ \ \ tmp\ =\ (uint16\_t)(((rxbuff[4]\ \&\ 0x0f)\ <<\ 8)\ |\ rxbuff[5]);}
\DoxyCodeLine{\ \ \ \ measure-\/>real\_current\ =\ CURRENT\_SMOOTH\_COEF\ *\ uint\_to\_float(tmp,\ T\_MIN,\ T\_MAX,\ 12)\ +}
\DoxyCodeLine{\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ (1\ -\/\ CURRENT\_SMOOTH\_COEF)\ *\ measure-\/>real\_current;}
\DoxyCodeLine{}
\DoxyCodeLine{\ \ \ \ \textcolor{keywordflow}{if}\ (!measure-\/>first\_feedback\_flag)\ \textcolor{comment}{//\ 初始化的时候设置偏置}}
\DoxyCodeLine{\ \ \ \ \{}
\DoxyCodeLine{\ \ \ \ \ \ \ \ measure-\/>first\_feedback\_flag\ =\ 1;}
\DoxyCodeLine{\ \ \ \ \ \ \ \ measure-\/>angle\_bias\ =\ measure-\/>total\_angle;}
\DoxyCodeLine{\ \ \ \ \ \ \ \ measure-\/>total\_angle\ =\ 0;}
\DoxyCodeLine{\ \ \ \ \ \ \ \ measure-\/>speed\_rads\ =\ 0;}
\DoxyCodeLine{\ \ \ \ \}}
\DoxyCodeLine{\}}

\end{DoxyCode}


第一次收到数据时默认电机处于限位处,将速度和角度都设置为零,记录当前的编码器数据,之后每次收到都减去该值. 